/**
 *  Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.plugins.Page;
import com.rshang.rsdp.common.Consts;
import com.rshang.rsdp.common.status.StatusCode;
import com.rshang.rsdp.entity.TbUser;
import com.rshang.rsdp.exception.RsdpSystemException;
import com.rshang.rsdp.kit.StrKits;
import com.rshang.rsdp.util.i18n.I18nMessage;

/**
 * 功能简介：前端控制器基类<br/>
 * @author zhangjun
 * @param <T>
 * @create 2017年7月31日下午2:35:33
 * 
 */
public class BaseController {

	@Autowired
	I18nMessage i18n;
	
	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;
	
	/**
	 * 获取当前session登陆用户对象
	 * @return
	 * 		TbUser
	 */
	protected TbUser getSessionUser(){
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()&&currentUser.getPrincipal()!=null){
			return (TbUser) currentUser.getPrincipal();
		}
		throw new RsdpSystemException(StatusCode.UNAUTHORIZED.getCode(),i18n.getMessage(StatusCode.UNAUTHORIZED.getKey()));
	}
	
	/**
	 * 获取shiro对象
	 * @return	Subject
	 */
	protected Subject getSubject(){
		return SecurityUtils.getSubject();
	}
	
	/**
	 * 获取请求分页参数
	 * @param size	默认每页显示记录数
	 * @return	Page
	 * 
	 */
	protected Page getPage(int size){
		int pageNo = StrKits.emptyToIntDefault(request.getParameter("page"),1);
		int pageSize = StrKits.emptyToIntDefault(request.getParameter("rows"),size);
		
		return new Page(pageNo,pageSize);
	}
	
	/**
	 * 生成分页对象 
	 * @return	Page
	 */
	@SuppressWarnings("rawtypes")
	protected Page getPage(){
		return getPage(Consts.PAGESIZE);
	}
	
	
}
